package com.homework.ch07;



import java.lang.reflect.Method;

/**
 * @author yang
 * @date 2024-03-26
 */
public class Test {
    public static void main(String[] args) throws Exception{
        Class<Student> studentClass = Student.class;
        Student zhangsan = studentClass.getConstructor().newInstance();
        Method setName = studentClass.getMethod("setName", String.class);
        setName.invoke(zhangsan, "张三");
        Method setAge = studentClass.getMethod("setAge", int.class);
        setAge.invoke(zhangsan,20);
        Method setScore = studentClass.getMethod("setScore", Double.class);
        setScore.invoke(zhangsan,100.0);

        Student lisi = studentClass.getConstructor(String.class, int.class, Double.class).newInstance("李四", 24, 90.0);

        Student[] allStudent = {zhangsan,lisi};
        for (Student student:allStudent) {
            System.out.println("姓名:"+student.getName());
            System.out.println("年龄:"+student.getAge());
            System.out.println("分数:"+student.getScore());
            System.out.println("----------------");
        }
    }
}
